/**
 * 自定义原创案例
 * */

var formList = [];


//列表
function getIndex(){
    var formData = {
        start: start,
        orderno: orderno,
        xqtitle: xqtitle,
        seller: seller,
        limit: pages.limit
    };
    $.ajax({
        type: apis.cxOrderIndex.method,
        url: apis.cxOrderIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var tbody = $('tbody'), h = '';
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formList = res.data;
            indexHtml();
            $('#pageTotal').html(res.total);
            var pageObj = pages; pageObj.total = res.total;
            pageHtml(pageObj);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}
//列表字符串
function indexHtml(){
    if (formList.length == 0) {
        msgPop({msg: '没有数据'}); return
    }
    var d = formList;
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].orderno + '</td>';
        h += '<td>' + d[i].case_name + '</td>';
        h += '<td><b>' + d[i].uname + '</b></td>';
        h += '<td>' + d[i].pay_way_name + '</td>';
        h += '<td>' + d[i].price + '</td>';
        if (d[i].status == 2) {
            h += '<td style="color: red;">' + d[i].status_name + '</td>';
        } else {
            h += '<td>' + d[i].status_name + '</td>';
        }
        h += '<td>' + d[i].addtime + '</td>';
        h += '<td>' + d[i].suretime + '</td>';
        h += '<td>' + d[i].finishtime + '</td>';
        h += '<td class="f-14">';
        //h += '<a href="javascript:;" onclick="lay_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        //h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        //h += '</a>';
        if (d[i].status == 1) {
            h += '<a href="javascript:;" onclick="lay_sure(' + d[i].id + ');">';
            h += '确认';
            h += '</a>';
        } else if (d[i].status == 4) {
            h += '<a href="javascript:;" onclick="lay_finish(' + d[i].id + ');">';
            h += '完成';
            h += '</a>';
        } else {
            //h += '-';
        }
        h += ' <br> ';
        h += '<a href="javascript:;" onclick="lay_show(\'查看详情\',\'order-show.html\', ' + d[i].id + ');">';
        h += '详情';
        h += '</a>';
        h += ' <br> ';
        h += '<a href="javascript:;" onclick="lay_upload(\'资料上传\',\'order-upload.html\', ' + d[i].id + ');">';
        h += '资料上传';
        h += '</a>';
        h += '<input type="hidden" id="status-' + d[i].id + '" value="' + d[i].status + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//确认弹窗
function lay_sure(v){
    var msg = '';
    msg += '一旦确认了，该需求订单的其他申请将失效！ <br>';
    msg += '确认要 <b style="color: green; font-size: 20px;">选择这个合作方</b> 吗？';
    layer.confirm(msg,function(index){
        setSure(v);
    });
}
function setSure(v){
    $.ajax({
        type: apis.cxOrderSure.method,
        url: apis.cxOrderSure.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            layer.msg('设置成功!',{icon: 1,time: 1000});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//完成弹窗
function lay_finish(v){
    var msg = '';
    msg += '确认要 <b style="color: green; font-size: 20px;">设置完成</b> 吗？';
    layer.confirm(msg,function(index){
        setFinish(v);
    });
}
function setFinish(v){
    $.ajax({
        type: apis.cxOrderFinish.method,
        url: apis.cxOrderFinish.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            layer.msg('设置成功!',{icon: 1,time: 1000});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//详情
function fileList(v){
    $.ajax({
        type: apis.cxOrderOne.method,
        url: apis.cxOrderOne.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var prefix = res.data.prefix;
            var imgList = res.data.imgList;
            var videoList = res.data.videoList;
            var zipList = res.data.zipList;
            var h = '', i;
            //图片
            h = '';
            for (i = 0; i < imgList.length; i ++) {
                h += '<img src="' + prefix + imgList[i] + '" style="margin: 5px;"/>';
            }
            $('#imgShow').html(h);
            $('#imgNum').html(imgList.length);
            //视频
            h = '';
            for (i = 0; i < videoList.length; i ++) {
                h += '<video width="320" height="240" class="videoContent" controls="" poster="" style="margin: 5px;">';
                h += '<source src="' + prefix + videoList[i] + '" type="video/mp4">';
                h += '</video>';
            }
            $('#videoShow').html(h);
            $('#videoNum').html(videoList.length);
            //压缩包
            h = '';
            for (i = 0; i < zipList.length; i ++) {
                h += '<a href="' + prefix + zipList[i].link + '" target="_blank" style="color: #0088CC;">';
                h += '<div style="margin: 10px;">';
                h += zipList[i].fname;
                h += '</div>';
                h += '</a>';
                h += '<br>';
            }
            $('#zipShow').html(h);
            $('#zipNum').html(zipList.length);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//上传弹窗
function lay_upload(title,url,id,w,h){
    url += '?id=' + id;
    var status = $('#status-' + id).val();
    if (status != 4) {
        msgPop({msg: '合作状态才能上传'}); return
    }
    layer_show(title,url,w,h);
}


//抓图
function uploadImg() {
    var fileObj = $('#imgUpload')[0], reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        var formData = {
            id: id, img: res.target.result,
        };
        //console.log(formData);
        //return
        $.ajax({
            type: apis.cxOrderUploadImg.method,
            url: apis.cxOrderUploadImg.url,
            data: formData,
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layer.msg(res.msg,{icon: 1,time: 1000});
                    return
                }
                layer.msg('上传成功',{icon: 1,time: 1000});
                setTimeout(function(){
                    location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误啦！');
            }
        });
    }
}


//视频上传
function uploadSp(){
    var uploadFile = $("#videoUpload")[0].files[0];
    var formData = new FormData();
    formData.append('id', id);
    formData.append("video", uploadFile);
    //var formData = {
    //    id: id,
    //    video: $("#videoUpload")[0].files[0],
    //};
    console.log(formData);
    //return
    $.ajax({
        type: apis.cxOrderUploadVideo.method,
        url: apis.cxOrderUploadVideo.url,
        data: formData,
        async: false,
        processData: false,
        contentType: false,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layer.msg(res.msg,{icon: 1,time: 1000});
                return
            }
            layer.msg('上传成功',{icon: 1,time: 1000});
            setTimeout(function(){
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//视频上传
function uploadZip(){
    var uploadFile = $("#zipUpload")[0].files[0];
    var formData = new FormData();
    formData.append('id', id);
    formData.append("zip", uploadFile);
    console.log(formData);
    //return
    $.ajax({
        type: apis.cxOrderUploadZip.method,
        url: apis.cxOrderUploadZip.url,
        data: formData,
        async: false,
        processData: false,
        contentType: false,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layer.msg(res.msg,{icon: 1,time: 1000});
                return
            }
            layer.msg('上传成功',{icon: 1,time: 1000});
            setTimeout(function(){
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//详情弹窗
function lay_show(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//详情
function showOne(v){
    $.ajax({
        type: apis.cxOrderOne.method,
        url: apis.cxOrderOne.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            var prefix = d.prefix;
            var imgList = d.imgList;
            var videoList = d.videoList;
            var zipList = d.zipList;
            $('#orderno').html(d.orderno);
            $('#xqtitle').html(d.xqtitle);
            $('#seller').html(d.seller);
            $('#pay_way_name').html(d.pay_way_name);
            $('#price').html(d.price);
            $('#status_name').html(d.status_name);
            $('#addtime').html(d.addtime);
            $('#suretime').html(d.suretime);
            $('#finishtime').html(d.finishtime);
            var h = '', i;
            //图片
            h = '';
            for (i = 0; i < imgList.length; i ++) {
                h += '<img src="' + prefix + imgList[i] + '" style="margin: 5px;"/>';
            }
            $('#imgOut').html(h);
            $('#imgNum').html(imgList.length);
            //视频
            h = '';
            for (i = 0; i < videoList.length; i ++) {
                h += '<video width="320" height="240" class="videoContent" controls="" poster="" style="margin: 5px;">';
                h += '<source src="' + prefix + videoList[i] + '" type="video/mp4">';
                h += '</video>';
            }
            $('#videoOut').html(h);
            $('#videoNum').html(videoList.length);
            //压缩包
            h = '';
            for (i = 0; i < zipList.length; i ++) {
                h += '<a href="' + prefix + zipList[i].link + '" target="_blank" style="color: #0088CC;">';
                h += '<div style="margin: 10px;">';
                h += zipList[i].fname;
                h += '</div>';
                h += '</a>';
                h += '<br>';
            }
            $('#zipOut').html(h);
            $('#zipNum').html(zipList.length);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


////删除
//function case_del(id){
//    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
//    layer.confirm(msg,function(index){
//        $.ajax({
//            type: apis.caseDel.method,
//            url: apis.caseDel.url,
//            data: { id: id },
//            success: function (res) {
//                console.log(res);
//                if (res.code < 0) {
//                    msgPop({msg: res.msg}); return
//                }
//                layer.msg('已删除!',{icon: 1,time: 1000});
//                setTimeout(function(){
//                    top.location.reload();
//                }, 1000);
//            },
//            error: function () {
//                console.log('错误了！');
//            }
//        });
//    });
//}